﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TpsAutoSurvey_PDA_Version_.Presenter;
using System.Collections;
using System.Reflection;
using System.Xml.Serialization;
using System.IO;

namespace TpsAutoSurvey_PDA_Version_.SmsModule
{
    public interface ICommandCenter
    {

        void OnMsgRecieved(string msg,string sender);
        void SendBackMsg(string str,string sender);
    }



    public abstract class CommandCenter : ICommandCenter
    {
        protected SurveyInterface controller;
        protected string comingNumber;
        protected Hashtable CommandList;
        void GenerateCMDList()
        {
            CommandList = controller.GetMethodList();
        }
        protected string GetCMDList()
        {
            //using (MemoryStream ms = new MemoryStream())
            //{
            //    XmlSerializer serialize = new XmlSerializer(typeof(Hashtable));
            //    serialize.Serialize(ms, CommandList);
            //    ms.Close();
            //    return Encoding.GetEncoding("gb2312").GetString(ms.ToArray(), 0, ms.ToArray().Length);
            //}

            string backlist = "";
            foreach (string s in CommandList.Keys)
            {
                backlist += CommandList[s].ToString() + ",";
            }
            return backlist;
        }
        public virtual CommandPara InvokeCommand(string cmdName, string comingNumber)
        {
            string MethodName;
            try
            {
                MethodName = CommandList[cmdName].ToString();
            }
            catch
            {
                SendBackMsg("未识别的命令!",comingNumber);
                return null;
            }
            CommandPara re = null;
            CommandPara p = new CommandPara(comingNumber, cmdName);
            Type cTpye = controller.GetType();
            MethodInfo method = cTpye.GetMethod(MethodName, new Type[] { typeof(CommandPara) });
            object[] para = new object[] { p };
            try
            {
              
                object result = method.Invoke(controller, para);
                re = result as CommandPara;
            }
                
            catch
            {
                re = new CommandPara(comingNumber,cmdName,  cmdName + "调用函数失败");
               
            }
            return re;
        }
        public CommandCenter(SurveyInterface c)
        {
            controller = c;
            GenerateCMDList();
            SetEvents();
        }

        private void SetEvents()
        {
            controller.OnerrHappen += new EventHandler(controller_OnerrHappen);
            controller.OnSurveyComplete += new EventHandler(controller_OnSurveyComplete);
        }

        void controller_OnSurveyComplete(object sender, EventArgs e)
        {
           CommandPara  p = sender as CommandPara;
           SendBackMsg(p.result, p.Sender);
        }
        void controller_OnerrHappen(object sender, EventArgs e)
        {
            CommandPara p = sender as CommandPara;
            SendBackMsg(p.result, p.Sender);
        }


        public virtual void SendBackCMDList()
        {

        }

        public virtual void OnMsgRecieved(string msg,string comingNumber)
        {
            if (msg.Length < 3)
            { return; }
            if (msg.Substring(0, 3) == "cmd")
            {
                if (msg.Length == 3)
                {
                    SendBackCMDList();
                }
                else
                {
                   CommandPara p= InvokeCommand(msg.Substring(3),comingNumber);
                   if (p != null)
                   {
                       SendBackMsg(p.result,p.Sender);
                   }
                }
            }
            else
            {
                return;
            }
        }

        public virtual void SendBackMsg(string str,string sender)
        {
            //sendsth
        }
    }
}
